//
//  PYQLoveListViewController.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2020/12/30.
//  Copyright © 2020 叁拾叁. All rights reserved.
//

import UIKit
import MJRefresh
import RxSwift
import RxDataSources
import Then
import SDCycleScrollView

class PYQLoveListViewController: SYBaseTableViewController {
    
    
    fileprivate var vm: VGTableViewPageViewModel<PYDianzanListModel>!
    
    var shenheDatas: [PYShenheListModel] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setTableView()
        setNoDataView()
        setupViewModel()
        self.title = "消息"
    }
    
    fileprivate func setupViewModel() {
        vm = VGTableViewPageViewModel<PYDianzanListModel>()
        vm.tableView = tableView
        vm.emptyViewParams = ("", "", 40.0, 30.0)
        tableView.vg_pageProtocol = vm
        vm.getList(type: .dianzanList(page: self.vm.vg_pageNo, size:1000), dispose: dispose)
        vm.getDataClosure = {[weak self] page in
            if let `self` = self {
                self.vm.getList(type: .dianzanList(page: self.vm.vg_pageNo, size:self.vm.size), dispose: self.dispose)
                self.requestShenhe {[weak self] (success) in
                    if let `self` = self {
                        self.loadDataSuccessed()
                    }
                }
            }
        }
        vm.loadDataClosure = {[weak self] success in
            if let `self` = self {
                if success {
                    self.loadDataSuccessed()
                }
            }
        }
        requestShenhe {[weak self] (success) in
            if let `self` = self {
                self.loadDataSuccessed()
            }
        }
        
    }
    
    func loadDataSuccessed() {
        DispatchQueue.main.async {
            self.setDataSource()
        }
    }
    
    func requestShenhe(complete:@escaping (Bool)->()) {
        self.shenheDatas.removeAll()
        requestVM.baseRequest(disposeBag: dispose, type: .shenheList, modelClass: SGBaseNoPageListModel<PYShenheListModel>.self) {[weak self] (res) in
            if let `self` = self {
                self.shenheDatas = res.data ?? []
                complete(true)
            }
            
        } Error: {
            complete(false)
        }
        
    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT), style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        self.view.addSubview(self.tableView!)
        
        self.tableView.rx.itemSelected
            .subscribe(onNext:{ [weak self] indexPath in
                if let `self` = self {
                    if self.shenheDatas.count > indexPath.row {
                        let id: String = self.shenheDatas[indexPath.row].id ?? ""
                        let vc = PYShenheViewController(id: id)
                        vc.backClosure = {[weak self] _ in
                            if let `self` = self {
                                self.requestShenhe {[weak self] (success) in
                                    if let `self` = self {
                                        self.loadDataSuccessed()
                                    }
                                }
                            }
                            
                        }
                        self.navigationController?.pushViewController(vc, animated: true)
                    }
                }
            })
            .disposed(by:self.dispose)
    }
    
    func setNoDataView() {
        noDataView = SYNoDataView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT))
        noDataView.isHidden = true
        tableView.addSubview(noDataView)
    }
    
    func setDataSource() {
        //初始化数据
        
        self.shenheDatas.forEach { (value) in
            var model = PYDianzanListModel()
            model.imgUrl = value.imgUrl
            model.createDate = value.createDate
            model.updateDate = value.updateDate
            self.vm.dataSource.append(model)
        }
        let sections = Observable.just([
            
            PYDianzanLisSection(header: "", items: self.vm.dataSource)
        ])
        
        //创建数据源
        let dataSource = RxTableViewSectionedAnimatedDataSource<PYDianzanLisSection>(
            //设置单元格
            configureCell: { ds, tv, ip, item in
                
                if self.shenheDatas.count > ip.row {
                    
                    let cell: NYWeiGuoCell = tv.dequeueReusableCell(withIdentifier: "Cell")
                        as? NYWeiGuoCell ?? NYWeiGuoCell(style: .default, reuseIdentifier: "Cell")
                    cell.configureView(item)
                    cell.selectionStyle = .none
                    return cell
                } else {
                    
                    let cell: NYLoveCell = tv.dequeueReusableCell(withIdentifier: "Cell")
                        as? NYLoveCell ?? NYLoveCell(style: .default, reuseIdentifier: "Cell")
                    cell.configureView(item)
                    cell.selectionStyle = .none
                    return cell
                }
                
                
            },
            //设置分区头标题
            titleForHeaderInSection: { ds, index in
                return ds.sectionModels[index].header
            })
        tableView.dataSource = nil
        //绑定单元格数据
        sections
            .bind(to: tableView.rx.items(dataSource: dataSource))
            .disposed(by: dispose)
        
        
    }
    
}



//自定义Section
struct PYDianzanLisSection {
    var header: String
    var items: [Item]
}

extension PYDianzanLisSection : AnimatableSectionModelType {
    typealias Item = PYDianzanListModel
    
    var identity: String {
        return header
    }
    
    init(original: PYDianzanLisSection, items: [Item]) {
        self = original
        self.items = items
    }
}

extension PYDianzanListModel: IdentifiableType {
    var identity: String {
        return id!
    }
    
    typealias Identity = String
}

struct PYShenheListSection {
    var header: String
    var items: [Item]
}

extension PYShenheListSection : AnimatableSectionModelType {
    typealias Item = PYShenheListModel
    
    var identity: String {
        return header
    }
    
    init(original: PYShenheListSection, items: [Item]) {
        self = original
        self.items = items
    }
}

extension PYShenheListModel: IdentifiableType {
    var identity: String {
        return id!
    }
    
    typealias Identity = String
}
